package cn.appsys.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.appsys.biz.DictionaryBiz;
import cn.appsys.biz.InfoBiz;
import cn.appsys.biz.VersionBiz;
import cn.appsys.entity.AppInfo;
import cn.appsys.entity.AppVersion;

@Controller
public class VersionController {

	@Autowired
	private VersionBiz versionBiz;
	
	@Autowired
	private DictionaryBiz dictionaryBiz;
	
	@Autowired
	private InfoBiz infoBiz;
	
	@RequestMapping("/addversion")
	public String addversion(int id,Model model){
		
		List<AppVersion> versionList=versionBiz.getByIdVersion(id);
		
		AppInfo appInfo=infoBiz.getByIdAppInfo(id);
		
		String versionName=dictionaryBiz.getByIdVersionStatus(appInfo.getStatus());
		
		
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("versionName", versionName);
		model.addAttribute("versionList", versionList);
		return "addversion";
	}
	
	@RequestMapping("/addversionsave")
	public String addversionsave(AppVersion version){
		version.setModifyDate(new Date());
		version.setPublishStatus(3);
		int temp=versionBiz.addVersion(version);
		System.out.println(temp);
		infoBiz.updateVersionId(version.getAppId(), version.getId());
		
		return "redirect:AppInfo";
	}
	
	@RequestMapping("/updateversion")
	public String updateversion(int id,Model model){
		
		List<AppVersion> versionList=versionBiz.getByIdVersion(id);
		
		AppInfo appInfo=infoBiz.getByIdAppInfo(id);
		AppVersion appVersion=versionBiz.getByDateVersion(id);
		String versionName=dictionaryBiz.getByIdVersionStatus(appInfo.getStatus());
		
		model.addAttribute("appVersion", appVersion);
		model.addAttribute("appInfo", appInfo);
		model.addAttribute("versionName", versionName);
		model.addAttribute("versionList", versionList);
		
		return "updateversion";
	}
	
	@RequestMapping(value="doupdateversion",method = RequestMethod.POST)
	public String doupdateversion(AppVersion appVersion){

		int temp = versionBiz.updateVersion(appVersion);
		infoBiz.updateVersionId(appVersion.getAppId(), appVersion.getId());
		return "redirect:AppInfo";
	}
}
